<script setup name="partner" lang="ts">
import {listPartner} from '@/api/hrp/partner';
import {PartnerVO} from "@/api/hrp/partner/types";
import {ElTable} from 'element-plus'
import {getDemo3,updateDemo3} from '@/api/demo/demo3';
import {Demo3Form, Demo3VO} from "@/api/demo/demo3/types";

const {proxy} = getCurrentInstance() as ComponentInternalInstance;
const {sys_yes_no, sys_user_sex} = toRefs<any>(proxy?.useDict('sys_yes_no', 'sys_user_sex'));

const props = defineProps({
  searchType: {
    type: Number,
    default: 1
  },
  demo3: {
    type: Object as PropType<Demo3VO>,
    default: null
  },
});

const pd = reactive({
      partnerOptions: <PartnerVO[]>([]),
      partnerList: <PartnerVO[]>([]),
      partnerIds: <(string | number)[]>([]),
      form: <Demo3Form>{},
      loading: false,
      open: false,
      total: 0,
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
    }
);

defineExpose({
  getPartnerList
});

async function getPartnerList() {
  const result = await getDemo3(props.demo3.id)
  pd.partnerIds = result.data.partnerIds;
  pd.loading = true;
  const res = await listPartner(pd.queryParams);
  pd.partnerOptions = res.rows;
  pd.total = res.total;
  pd.loading = false;
  pd.open = true;
  selectTableByPartnerId();
}

function selectTableByPartnerId() {
  nextTick(() => {
    for (const partnerId of pd.partnerIds) {
      for (const item of pd.partnerOptions) {
        if (item.id === partnerId) {
          (proxy?.$refs["partnerTable"] as typeof ElTable).toggleRowSelection(item, true);
        }
      }
    }
  })
}

/** 提交按钮 */
async function submitForm() {
  pd.form.id = props.demo3.id;
  pd.form.name = props.demo3.name;
  pd.form.status = props.demo3.status;
  pd.form.remark = props.demo3.remark;
  await updateDemo3(pd.form)
  proxy?.$modal.msgSuccess("修改成功");
  await getPartnerList();
}

const handleSelectionChange = (val: PartnerVO[]) => {
  pd.form.partnerIds = [];
  for (let item of val) {
    if (!pd.form.partnerIds.includes(item.id as string)) {
      pd.form.partnerIds.push(item.id as string);
    }
  }
}
</script>

<template>
  <div>
    <el-link v-if="props.searchType===1" type="primary" @click="getPartnerList">
      {{ props.demo3.name }}
    </el-link>
    <el-dialog title="关联伙伴" v-model="pd.open" width="1000px" append-to-body>
      <el-table ref="partnerTable" v-loading="pd.loading" :data="pd.partnerOptions" row-key="id"
                @selection-change="handleSelectionChange">
        <el-table-column type="selection" width="55" :reserve-selection="true" align="center"/>
        <el-table-column label="上级" align="center" prop="parentId"/>
        <el-table-column label="名称" align="center" prop="name"/>
        <el-table-column label="编码" align="center" prop="code"/>
        <el-table-column label="性别" align="center" prop="sex">
          <template #default="scope">
            <dict-tag :options="sys_user_sex" :value="scope.row.sex"/>
          </template>
        </el-table-column>
        <el-table-column label="手机号" align="center" prop="mobile"/>
        <el-table-column label="是否启用" align="center" prop="status">
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.status"/>
          </template>
        </el-table-column>
        <el-table-column label="固定电话" align="center" prop="telephone"/>
        <el-table-column label="邮箱" align="center" prop="email"/>
        <el-table-column label="标识" align="center" prop="logo"/>
        <el-table-column label="网站地址" align="center" prop="website"/>
        <el-table-column label="身份证号" align="center" prop="idCard"/>
        <el-table-column label="生日" align="center" prop="birthday" width="180">
          <template #default="scope">
            <span>{{ parseTime(scope.row.birthday, '{y}-{m}-{d}') }}</span>
          </template>
        </el-table-column>
        <el-table-column label="评分" align="center" prop="score"/>
        <el-table-column label="地址" align="center" prop="address"/>
        <el-table-column label="是否客户" align="center" prop="isCustomer">
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.isCustomer"/>
          </template>
        </el-table-column>
        <el-table-column label="是否供应商" align="center" prop="isSupplier">
          <template #default="scope">
            <dict-tag :options="sys_yes_no" :value="scope.row.isSupplier"/>
          </template>
        </el-table-column>
        <el-table-column label="图片" align="center" prop="picture"/>
        <el-table-column label="备注" align="center" prop="remark"/>
      </el-table>

      <pagination
          v-show="pd.total>0"
          :total="pd.total"
          v-model:page="pd.queryParams.pageNum"
          v-model:limit="pd.queryParams.pageSize"
          @pagination="getPartnerList"
      />
      <template #footer>
        <div class="dialog-footer">
          <el-button type="success" @click="submitForm">修 改</el-button>
          <el-button type="info" @click="pd.open = false">关 闭</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<style scoped>

</style>
